#!/bin/bash
########################################
#  This file is part of the open-eBackup project.
# Copyright (c) 2024 Huawei Technologies Co.,Ltd.
#
# open-eBackup is licensed under MPL v2.
# You can use this software according to the terms and conditions of the MPL v2.
# You may obtain a copy of MPL v2 at:
#
#          https://www.mozilla.org/en-US/MPL/2.0
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the MPL v2 for more details.
########################################

set -ex
set -o pipefail
install_dir=$1

ls Python-3.9.11.tgz

# clear
[ -d "${install_dir}" ] && rm -rf ${install_dir}
mkdir -p ${install_dir}


# check or install denpendence
tmp_cpus=$(grep -w processor /proc/cpuinfo | wc -l)

SSL_PATH=/opt/buildtools/openssl
tar -xf openssl-1.1.1q.tar.gz
pushd openssl-1.1.1q
./config shared --prefix=$SSL_PATH shared zlib
make -j ${tmp_cpus} && make install
export LD_LIBRARY_PATH="$SSL_PATH/lib:$LD_LIBRARY_PATH"
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH" >>  /etc/profile
popd

if [ $OS_TYPE == "CentOS-6" ];then
    tar -xf Python-3.9.0.tgz
    cd Python-3.9.0/
else
    tar -xf Python-3.9.11.tgz
    cd Python-3.9.11/
fi
./configure --enable-loadable-sqlite-extensions --enable-shared --prefix=${install_dir} --with-openssl=$SSL_PATH --with-openssl-rpath=auto
make -j ${tmp_cpus}
make install

#link 
ln -sf ${install_dir}/bin/python3.9 /usr/local/bin/python3
ln -sf ${install_dir}/bin/python3.9 /usr/local/bin/python
ln -sf ${install_dir}/bin/pip3.9 /usr/local/bin/pip3
rm -f /usr/lib64/libpython3.9.so.1.0
find ${install_dir} -name libpython3.9.so.1.0 | xargs -i cp -u {} /usr/lib64/

#set env
sed -i '/PYTHON_HOME/d' /etc/profile
echo "export PYTHON_HOME=${install_dir}" >> /etc/profile
echo "export PATH=${install_dir}/bin:\$PATH" >>/etc/profile
cd ../

echo "current path is:"`pwd`
# unzip depend package
tar -xvf idna-2.10.tar.gz
tar -xvf chardet-4.0.0.tar.gz
tar -xvf certifi-2021.5.30.tar.gz
tar -xvf urllib3-1.26.5.tar.gz
tar -xvf requests-2.25.1.tar.gz

echo "import ssl" >> ssl.py
python ssl.py
rm -f ssl.py

## install pyinstaller
pip3 install pyinstaller_hooks_contrib-2022.8-py2.py3-none-any.whl

tar -xvf pexpect-4.8.0.tar.gz
tar -xvf ptyprocess-0.7.0.tar.gz
tar -xvf pyinstaller-4.0.tar.gz
tar -xvf altgraph-0.17.2.tar.gz

# install Pyinstaller
cd altgraph-0.17.2
python3 setup.py build
python3 setup.py install
cd ..

cd pyinstaller-4.0
cd bootloader
python3 ./waf all
cd ..
python3 setup.py build
python3 setup.py install
export PATH=${install_dir}/bin:$PATH
echo 'print("1")' > 1.py
pyinstaller -F 1.py
cd ..

## install pexpect
cd pexpect-4.8.0/
python3 setup.py build
python3 setup.py install
cd ..

## install ptyprocess
cd ptyprocess-0.7.0/
python3 setup.py build
python3 setup.py install
cd ..

# install idna
pushd idna-2.10/
python3 setup.py build
python3 setup.py install
popd

# install chardet
pushd chardet-4.0.0/
python3 setup.py build
python3 setup.py install
popd

# install certifi
pushd certifi-2021.5.30/
python3 setup.py build
python3 setup.py install
popd

# install urllib3
pushd urllib3-1.26.5/
python3 setup.py build
python3 setup.py install
popd

# install requests
pushd requests-2.25.1/
python3 setup.py build
python3 setup.py install
